<?php

/**
 * @author @author Samrat Khan & Sadik Sarfaraz - May 6, 2013
 */
require 'foo.config.php';

if ($_REQUEST['action'] == 'modifySI') {
    $siid = $_GET['siid'];
    $session_id = session_id();
    $sess_cusID = $_SESSION['user']['id'];

    $query = "UPDATE stockin SET session_id = '" . $sess_cusID . "', junkentry = '1' WHERE stockin_id = '" . $siid . "' AND session_id = '" . $session_id . "' ";
    try {
        $stmt = $db->prepare($query);
        $result = $stmt->execute($query_params);
    } catch (PDOException $ex) {
        die("Failed to run query: " . $ex->getMessage());
    }

    $rows = selectAllWhereAA('stockin', 'session_id', $session_id, $db);

    echo "<table class=\"row-fluid table table-striped table-bordered\">";
            echo "<tr>";
            echo "<th>" . $optionsIcons['optionIcon'] . "</th>";
            echo "<th>" . $stock_in['serialno'] . "</th>";
            echo "<th>" . $stock_in['project_id'] . "</th>";
            echo "<th>" . $stock_in['productid'] . "</th>";
            echo "<th>" . $stock_in['description'] . "</th>";
            echo "<th>" . $stock_in['revisionstate'] . "</th>";
            echo "<th>" . $stock_in['shiptype'] . "</th>";
            echo "<th>" . $stock_in['in_quantity'] . "</th>";
            echo "<th>" . $stock_in['uom'] . "</th>";
            echo "<th>" . $stock_in['invoicenumber'] . "</th>";
            echo "<th>" . $stock_in['shipid'] . "</th>";
            echo "<th>" . $stock_in['mawb'] . "</th>";
            echo "<th>" . $stock_in['hawb'] . "</th>";
            echo "<th>" . $stock_in['receiveddate'] . "</th>";
            echo "<th>" . $stock_in['shipfrom'] . "</th>";
            echo "<th>" . $stock_in['boxno'] . "</th>";
            echo "<th>" . $stock_in['rma'] . "</th>";
            echo "<th>" . $stock_in['warehouse_name'] . "</th>";
            echo "<th>" . $stock_in['rackno'] . "</th>";
            echo "<th>" . $stock_in['docketnumber'] . "</th>";
            echo "<th>" . $stock_in['manufacturingdate'] . "</th>";
            echo "<th>" . $stock_in['pickuprequestdate'] . "</th>";
            echo "<th>" . $stock_in['employee_id'] . "</th>";
            echo "<th>" . $stock_in['eng_id'] . "</th>";
            echo "<th>" . $stock_in['siteid'] . "</th>";
            echo "<th>" . $stock_in['docketdate'] . "</th>";
            echo "<th>" . $stock_in['remarks'] . "</th>";
            echo "<th>" . $stock_in['userid'] . "</th>";
            echo "<th>" . $stock_in['cus_id'] . "</th>";
            echo "<th>" . $stock_in['inserteddate'] . "</th>";
            echo "</tr>";
            foreach ($rows as $row) {
                echo "<tr>";
                echo "<td>";
                echo "<a class='btn btn-danger btn-mini' href='javascript:void(0)' onclick='deletestockin(" . $row["stockin_id"] . ")'>" . $optionsIcons['delete'] . "</a>";
                echo "&nbsp;";
                echo "<a class='btn btn-success btn-mini' href='javascript:void(0)' onclick='savestockin(" . $row["stockin_id"] . ")'>" . $optionsIcons['save'] . "</a>";
                echo "</td>";
                echo "<td>" . $row['serialno'] . "</td>";
                echo "<td>";
                echo getName('projects', 'projectname', 'project_id', $row['project_id'], $db);
                echo "<br/>";
                $idofcustomer = getName('projects', 'cus_id', 'project_id', $row['project_id'], $db);
                $customerid = getName('customerinfo', 'cus_id', 'cus_id', $idofcustomer, $db);
                echo "</td>";
                echo "<td>" . $row['productid'] . "</td>";
                echo "<td>" . $row['description'] . "</td>";
                echo "<td>" . $row['revisionstate'] . "</td>";
                echo "<td>" . getName('shiptype', 'typename', 'shiptype_id', $row['shiptype'], $db) . "</td>";
                echo "<td>" . $row['in_quantity'] . "</td>";
                echo "<td>" . getName('uom', 'measurement', 'uom_id', $row['uom'], $db) . "</td>";
                echo "<td>" . $row['invoicenumber'] . "</td>";
                echo "<td>" . $row['shipid'] . "</td>";
                echo "<td>" . $row['mawb'] . "</td>";
                echo "<td>" . $row['hawb'] . "</td>";
                echo "<td>" . $row['receiveddate'] . "</td>";
                echo "<td>" . $row['shipfrom'] . "</td>";
                echo "<td>" . $row['boxno'] . "</td>";
                echo "<td>" . $row['rma'] . "</td>";
                echo "<td>" . getName('warehouses', 'warehousename', 'warehouse_id', $row['warehouse_id'], $db) . "</td>";
                echo "<td>" . $row['rackno'] . "</td>";
                echo "<td>" . $row['docketnumber'] . "</td>";
                echo "<td>" . $row['manufacturingdate'] . "</td>";
                echo "<td>" . $row['pickuprequestdate'] . "</td>";
                echo "<td>" . getName('employee', 'employeename', 'employee_id', $row['employee_id'], $db) . "</td>";
                echo "<td>" . getName('engineerinfo', 'engname', 'eng_id', $row['eng_id'], $db) . "</td>";
                echo "<td>" . $row['siteid'] . "</td>";
                echo "<td>" . $row['docketdate'] . "</td>";
                echo "<td>" . $row['remarks'] . "</td>";
                echo "<td>" . getName('wms_users', 'username', 'id', $row['userid'], $db) . "</td>";
                echo "<td>" . getName('customerinfo', 'cusname', 'cus_id', $row['cus_id'], $db) . "</td>";
                echo "<td>" . $row['inserteddate'] . "</td>";
                echo "</tr>";
            }
            echo "<table>";
}
if ($_REQUEST['action'] == 'modifySO') {
    $soid = $_GET['soid'];
    $session_id = session_id();
    $sess_userID = $_SESSION['user']['id'];

    $query = "UPDATE stockout SET session_id = '" . $sess_userID . "', junkentry = '1' WHERE stockout_id = '" . $soid . "' AND session_id = '" . $session_id . "' ";
    try {
        $stmt = $db->prepare($query);
        $result = $stmt->execute($query_params);
    } catch (PDOException $ex) {
        die("Failed to run query: " . $ex->getMessage());
    }
    $rows = selectAllWhereAA('stockout', 'session_id', $session_id, $db);
    //var_dump($rows);
    echo "<table class=\"form-inline row-fluid table table-striped table-bordered\">";
    echo "<tr>";
    echo "<th>" . $optionsIcons['option'] . "</th>";
    echo "<th>" . $stock_out['serialno'] . "</th>";
    echo "<th>" . $stock_out['productid'] . "</th>";
    echo "<th>" . $stock_out['boxno'] . "</th>";
    echo "<th>" . $stock_out['description'] . "</th>";
    echo "<th>" . $stock_out['eng_id'] . "</th>";
    echo "<th>" . $stock_out['responsetype'] . "</th>";
    echo "<th>" . $stock_out['revisionstate'] . "</th>";
    echo "<th>" . $stock_out['challannumber'] . "</th>";
    echo "<th>" . $stock_out['out_quantity'] . "</th>";
    echo "<th>" . $stock_out['uom'] . "</th>";
    echo "<th>" . $stock_out['requestnumber'] . "</th>";
    echo "<th>" . $stock_out['requestedby'] . "</th>";
    echo "<th>" . $stock_out['siteid'] . "</th>";
    echo "<th>" . $stock_out['project_id'] . "</th>";
    echo "<th>" . $stock_out['requesttime'] . "</th>";
    echo "<th>" . $stock_out['deliverytime'] . "</th>";
    echo "<th>" . $stock_out['receivedtime'] . "</th>";
    echo "<th>" . $stock_out['sla'] . "</th>";
    echo "<th>" . $stock_out['receivername'] . "</th>";
    echo "<th>" . $stock_out['cellnumber'] . "</th>";
    echo "<th>" . $stock_out['warehouseid'] . "</th>";
    echo "<th>" . $stock_out['transportername'] . "</th>";
    echo "<th>" . $stock_out['employee_id'] . "</th>";
    echo "<th>" . $stock_out['remarks'] . "</th>";
    echo "<th>" . $stock_out['user_id'] . "</th>";
    echo "<th>" . $stock_out['cus_id'] . "</th>";
    echo "<th>" . $stock_out['inserteddate'] . "</th>";
    echo "</tr>";
    foreach ($rows as $row) {
        echo "<tr>";
        echo "<td>";
        echo "<a class='btn btn-danger btn-mini' href='javascript:void(0)' onclick='deletestockout(" . $row["stockout_id"] . ")'>" . $optionsIcons['delete'] . "</a>";
        echo "&nbsp;";
        echo "<a class='btn btn-success btn-mini' href='javascript:void(0)' onclick='savestockout(" . $row["stockout_id"] . ")'>" . $optionsIcons['save'] . "</a>";
        echo "</td>";
        echo "<td>" . $row['serialno'] . "</td>";
        echo "<td>" . $row['productid'] . "</td>";
        echo "<td>" . $row['boxno'] . "</td>";
        echo "<td>" . $row['description'] . "</td>";
        echo "<td>" . getName('engineerinfo', 'engname', 'eng_id', $row['eng_id'], $db) . "</td>";
        echo "<td>" . getName('responsetype', 'uniquename', 'responsetype_id', $row['response_type_id'], $db) . "</td>";
        echo "<td>" . $row['revisionstate'] . "</td>";
        echo "<td>" . $row['challannumber'] . "</td>";
        echo "<td>" . $row['out_quantity'] . "</td>";
        echo "<td>" . getName('uom', 'measurement', 'uom_id', $row['uom'], $db) . "</td>";
        echo "<td>" . $row['requestnumber'] . "</td>";
        echo "<td>" . $row['requestedby'] . "</td>";
        echo "<td>" . $row['siteid'] . "</td>";
        echo "<td>" . getName('projects', 'projectname', 'project_id', $row['project_id'], $db) . "</td>";
        echo "<td>" . $row['requesttime'] . "</td>";
        echo "<td>" . $row['deliverytime'] . "</td>";
        echo "<td>" . $row['receivedtime'] . "</td>";
        echo "<td>" . $row['sla'] . "</td>";
        echo "<td>" . $row['receivername'] . "</td>";
        echo "<td>" . $row['cellnumber'] . "</td>";
        echo "<td>" . getName('warehouses', 'warehousename', 'warehouse_id', $row['warehouseid'], $db) . "</td>";
        echo "<td>" . $row['transportername'] . "</td>";
        echo "<td>" . getName('employee', 'employeename', 'employee_id', $row['employee_id'], $db) . "</td>";
        echo "<td>" . $row['remarks'] . "</td>";
        echo "<td>" . getName('wms_users', 'username', 'id', $row['userid'], $db) . "</td>";
        echo "<td>" . getName('customerinfo', 'cusname', 'cus_id', $row['cus_id'], $db) . "</td>";
        echo "<td>" . $row['inserteddate'] . "</td>";
        echo "</tr>";
    }
    echo "<table>";
//    $sess_cusID = $_SESSION['user']['cus_id'];    
//    $queryReturn = "INSERT INTO returnnote ( stockout_id, backstatus, cus_id, inserteddate) VALUES ( :stockout_id, :backstatus, :cus_id, :inserteddate)";
//    $query_params_return = array(
//        ':stockout_id'      => $soid,
//        ':backstatus'       => 1,
//        ':cus_id'           => $sess_cusID,
//        ':inserteddate'     => date('Y-m-d H:i:s')
//    );
//    try {
//        $stmt1 = $db->prepare($queryReturn);
//        $result = $stmt1->execute($query_params_return);
//        var_dump($stmt1);
//    } catch (PDOException $ex) {
//        die("Failed to run query: ". $ex->getMessage());
//    }
}
if ($_REQUEST['action'] == 'modifyReturn') {
    $faulties_id = $_GET['faulties_id'];
    $session_id = session_id();
    $sess_cusID = $_SESSION['user']['id'];

    $query = "UPDATE faulties SET session_id = '" . $sess_cusID . "', junkentry = '1' WHERE faulties_id = '" . $faulties_id . "' AND session_id = '" . $session_id . "' ";
    try {
        $stmt = $db->prepare($query);
        $result = $stmt->execute($query_params);
    } catch (PDOException $ex) {
        die("Failed to run query: " . $ex->getMessage());
    }

    if ($result) {
        $session_id = session_id();
        $rows = selectAllWhereAA('faulties', 'session_id', $session_id, $db);

        echo "<table class=\"row-fluid table table-striped table-bordered\">";
        echo "<tr>";
        echo "<th>" . $optionsIcons['optionIcon'] . "</th>";
        echo "<th>" . $faulties['serialno'] . "</th>";
        echo "<th>" . $faulties['project_id'] . "</th>";
        echo "<th>" . $faulties['rma'] . "</th>";
        echo "<th>" . $faulties['pickuprequestdate'] . "</th>";
        echo "<th>" . $faulties['physicallyreceivedate'] . "</th>";
        echo "<th>" . $faulties['productid'] . "</th>";
        echo "<th>" . $faulties['description'] . "</th>";
        echo "<th>" . $faulties['revisionstate'] . "</th>";
        echo "<th>" . $faulties['boxno'] . "</th>";
        echo "<th>" . $faulties['uom'] . "</th>";
        echo "<th>" . $faulties['return_quantity'] . "</th>";
        echo "<th>" . $faulties['manufacturingdate'] . "</th>";
        echo "<th>" . $faulties['faultiesstatus'] . "</th>";
        echo "<th>" . $faulties['siteid'] . "</th>";
        echo "<th>" . $faulties['warehouseid'] . "</th>";
        echo "<th>" . $faulties['employeeid'] . "</th>";
        echo "<th>" . $faulties['docketdate'] . "</th>";
        echo "<th>" . $faulties['return_docketnumber'] . "</th>";
        echo "<th>" . $faulties['remarks'] . "</th>";
        echo "<th>" . $faulties['userid'] . "</th>";
        echo "<th>" . $customer_info['cusname'] . "</th>";
        echo "<th>" . $faulties['inserteddate'] . "</th>";
        echo "</tr>";
        foreach ($rows as $row) {
            echo "<tr>";
            echo "<td>";
            echo "<a class='btn btn-danger btn-mini' href='javascript:void(0)' onclick='deletereturn(" . $row["faulties_id"] . ")'>" . $optionsIcons['delete'] . "</a>";
            echo "&nbsp;";
            echo "<a class='btn btn-success btn-mini' href='javascript:void(0)' onclick='savereturn(" . $row["faulties_id"] . ")'>" . $optionsIcons['save'] . "</a>";
            echo "</td>";
            echo "<td>" . $row['serialno'] . "</td>";
            echo "<td>";
            echo getName('projects', 'projectname', 'project_id', $row['project_id'], $db);
            echo "<br/>";
            $idofcustomer = getName('projects', 'cus_id', 'project_id', $row['project_id'], $db);
            $customerid = getName('customerinfo', 'cus_id', 'cus_id', $idofcustomer, $db);
            echo "</td>";
            echo "<td>" . $row['rma'] . "</td>";
            echo "<td>" . $row['pickuprequestdate'] . "</td>";
            echo "<td>" . $row['physicallyreceivedate'] . "</td>";
            echo "<td>" . $row['productid'] . "</td>";
            echo "<td>" . $row['description'] . "</td>";
            echo "<td>" . $row['revisionstate'] . "</td>";
            echo "<td>" . $row['boxno'] . "</td>";
            echo "<td>" . getName('uom', 'measurement', 'uom_id', $row['uom'], $db) . "</td>";
            echo "<td>" . $row['return_quantity'] . "</td>";
            echo "<td>" . $row['manufacturingdate'] . "</td>";
            echo "<td>" . getName('faultystatus', 'statusname', 'status_id', $row['faultiesstatus'], $db) . "</td>";
            echo "<td>" . $row['siteid'] . "</td>";
            echo "<td>" . getName('warehouses', 'warehousename', 'warehouse_id', $row['warehouseid'], $db) . "</td>";
            echo "<td>" . getName('employee', 'employeename', 'employee_id', $row['employeeid'], $db) . "</td>";
            echo "<td>" . $row['docketdate'] . "</td>";
            echo "<td>" . $row['faulties_docketnumber'] . "</td>";
            echo "<td>" . $row['remarks'] . "</td>";
            echo "<td>" . getName('wms_users', 'username', 'id', $row['userid'], $db) . "</td>";
            echo "<td>" . getName('customerinfo', 'cusname', 'cus_id', $row['cus_id'], $db) . "</td>";
            echo "<td>" . $row['inserteddate'] . "</td>";
            echo "</tr>";
        }
        echo "<table>";
    }
}
if ($_REQUEST['action'] == 'modifyExport') {
    $faulty_export_id = $_GET['faulty_export_id'];
    $session_id = session_id();
    $sess_cusID = $_SESSION['user']['id'];

    $query = "UPDATE faulty_export SET session_id = '" . $sess_cusID . "', junkentry = '1' WHERE faulty_export_id = '" . $faulty_export_id . "' AND session_id = '" . $session_id . "' ";
    try {
        $stmt = $db->prepare($query);
        $result = $stmt->execute($query_params);
    } catch (PDOException $ex) {
        die("Failed to run query: " . $ex->getMessage());
    }
    $session_id = session_id();
    $rows = selectAllWhereAA('faulty_export', 'session_id', $session_id, $db);

    echo "<table class=\"row-fluid table table-striped table-bordered\">";
    echo "<tr>";
    echo "<th>" . $optionsIcons['optionIcon'] . "</th>";
    echo "<th>" . $return['serialno'] . "</th>";
    echo "<th>" . $return['project_id'] . "</th>";
    echo "<th>" . $return['return_rma'] . "</th>";
    echo "<th>" . $return['pickuprequestdate'] . "</th>";
    echo "<th>" . $return['physicallyreceivedate'] . "</th>";
    echo "<th>" . $return['productid'] . "</th>";
    echo "<th>" . $return['description'] . "</th>";
    echo "<th>" . $return['revisionstate'] . "</th>";
    echo "<th>" . $return['boxno'] . "</th>";
    echo "<th>" . $return['uom'] . "</th>";
    echo "<th>" . $return['return_quantity'] . "</th>";
    echo "<th>" . $return['manufacturingdate'] . "</th>";
    echo "<th>" . $return['returningstatus'] . "</th>";
    echo "<th>" . $return['receivedfrom'] . "</th>";
    echo "<th>" . $return['siteid'] . "</th>";
    echo "<th>" . $return['warehouse_id'] . "</th>";
    echo "<th>" . $return['employee_id'] . "</th>";
    echo "<th>" . $return['docketdate'] . "</th>";
    echo "<th>" . $return['return_docketnumber'] . "</th>";
    echo "<th>" . $return['remarks'] . "</th>";
    echo "<th>" . $return['userid'] . "</th>";
    echo "<th>" . $return['cus_id'] . "</th>";
    echo "<th>" . $return['inserteddate'] . "</th>";
    echo "</tr>";
    foreach ($rows as $row) {
        echo "<tr>";
        echo "<td>";
        echo "<a class='btn btn-danger btn-mini' href='javascript:void(0)' onclick='deleteexport(" . $row["faulty_export_id"] . ")'>" . $optionsIcons['delete'] . "</a>";
        echo "&nbsp;";
        echo "<a class='btn btn-success btn-mini' href='javascript:void(0)' onclick='saverexport(" . $row["faulty_export_id"] . ")'>" . $optionsIcons['save'] . "</a>";
        echo "</td>";
        echo "<td>" . $row['serialno'] . "</td>";
        echo "<td>";
        echo getName('projects', 'projectname', 'project_id', $row['project_id'], $db);
        echo "<br/>";
        $idofcustomer = getName('projects', 'cus_id', 'project_id', $row['project_id'], $db);
        $customerid = getName('customerinfo', 'cus_id', 'cus_id', $idofcustomer, $db);
        echo "</td>";
        echo "<td>" . $row['productid'] . "</td>";
        echo "<td>" . $row['description'] . "</td>";
        echo "<td>" . $row['revisionstate'] . "</td>";
        echo "<td>" . $row['boxno'] . "</td>";
        echo "<td>" . getName('uom', 'measurement', 'uom_id', $row['uom'], $db) . "</td>";
        echo "<td>" . $row['faulty_export_quantity'] . "</td>";
        echo "<td>" . $row['handedoverto'] . "</td>";
        echo "<td>" . $row['manufacturingdate'] . "</td>";
        echo "<td>" . $row['faultydocketnumber'] . "</td>";
        echo "<td>" . $row['docketdate'] . "</td>";
        echo "<td>" . $row['siteid'] . "</td>";
        echo "<td>" . getName('warehouses', 'warehousename', 'warehouse_id', $row['warehouse_id'], $db) . "</td>";
        echo "<td>" . $row['receivedfrom'] . "</td>";
        echo "<td>" . $row['rr_rma'] . "</td>";
        echo "<td>" . $row['export_rma'] . "</td>";
        echo "<td>" . $row['rr_invoiceno'] . "</td>";
        echo "<td>" . $row['rr_date'] . "</td>";
        echo "<td>" . $row['rr_mawb'] . "</td>";
        echo "<td>" . $row['rr_awb'] . "</td>";
        echo "<td>" . $row['remarks'] . "</td>";
        echo "<td>" . getName('wms_users', 'username', 'id', $row['userid'], $db) . "</td>";
        echo "<td>" . getName('customerinfo', 'cusname', 'cus_id', $row['cus_id'], $db) . "</td>";
        echo "<td>" . $row['inserteddate'] . "</td>";
        echo "</tr>";
    }
    echo "<table>";
}
if ($_REQUEST['action'] == 'modifyReturnNote') {
    $note_id = $_GET['note_id'];
    $session_id = session_id();
    $sess_cusID = $_SESSION['user']['id'];

    $query = "UPDATE returnnote SET backstatus = '0' WHERE note_id = '" . $note_id . "' ";
    try {
        $stmt = $db->prepare($query);
        $result = $stmt->execute($query_params);
    } catch (PDOException $ex) {
        die("Failed to run query: " . $ex->getMessage());
    }
    $sess_cusID = $_SESSION['user']['cus_id'];
    $rows = pickReturnStatus($sess_cusID, $db);

    echo "<table id=\"stockinhanddiv\" class=\"row-fluid table table-striped table-bordered\">";
    echo "<tr>";
    echo "<th>" . $stock_in['serialno'] . "</th>";
    echo "<th>" . $stock_in['productid'] . "</th>";
    echo "<th>" . $stock_out['siteid'] . "</th>";
    echo "<th>" . $stock_out['receivedtime'] . "</th>";
    echo "<th>" . $stock_out['out_quantity'] . "</th>";
    echo "<th>" . $stock_out['uom'] . "</th>";
    echo "<th>" . $stock_out['stockout_id'] . "</th>";
    echo "<th>" . $stock_in['inserteddate'] . "</th>";
    echo "<th>" . "Update as Received" . "</th>";
    echo "</tr>";
    foreach ($rows as $row) {
        echo "<tr>";
        echo "<td>" . $row['serialno'] . "</td>";
        echo "<td>" . $row['productid'] . "</td>";
        echo "<td>" . $row['siteid'] . "</td>";
        echo "<td>" . $row['receivedtime'] . "</td>";
        echo "<td>" . $row['out_quantity'] . "</td>";
        echo "<td>" . getName('uom', 'measurement', 'uom_id', $row['uom'], $db) . "</td>";
        echo "<td>" . $row['stockout_id'] . "</td>";
        echo "<td>" . $row['inserteddate'] . "</td>";
        echo "<td>" . "<a href='javascript:void(0)' class='btn btn-success btn-small' onclick='receivedok(" . $row['note_id'] . ")'><i class='icon-ok icon-white'></i>&nbsp; Tick</a>" . "</td>";
        echo "</tr>";
    }
    echo "<table>";
}
?>
